﻿/**
 * Router Beta AS3 Framework
 * @version 1.0
 * @since 2010
 */
package com.routerbeta.net
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestHeader;
	
	import com.routerbeta.net.events.FileEvent;
	
	/**
	 * Necessária a framework as3corelib:
	 * http://as3corelib.googlecode.com/svn/trunk/
	 */
	import com.adobe.images.JPGEncoder;
	
	/**
	 * Uploader de imagens.
	 * 
	 * @version 1.0
	 * @date 18/02/2010
	 * 
	 * @author Will Soares @ willsoares.com
	 * @since 18/02/2010
	 */
	public class ImageUploader extends FileUploader
	{
		
		public function get quality():uint
		{
			return _quality;
		}
		public function set quality(value:uint):void
		{
			_quality = value;
		}
		private var _quality:uint = 80;
		
		public function get image():Bitmap
		{
			return content as Bitmap;
		}
		
		public function get bitmap():BitmapData
		{
			if (!_bitmap){
				_bitmap = new BitmapData(image.width, image.height, false, 0xffffff);
				_bitmap.draw(image);
			}
				
			return _bitmap;
		}
		private var _bitmap:BitmapData;
		
		public function ImageUploader(quality:uint = 80, propagateEvents = false):void
		{
			super(propagateEvents);
			
			_quality = quality;
			addEventListener("select", onSelect);
		}
		
		private function onSelect(e:FileEvent):void 
		{
			open();
		}
		
		public function resize(width:uint, height:uint, constrain:Boolean = true):void
		{
			image.smoothing = true;
			
			if(constrain)
				if (image.width > image.height)
				{
					image.height = height;
					image.scaleX = image.scaleY;
				} else {
					image.width = width;
					image.scaleY = image.scaleX;
				}
			else {
				image.width = width;
				image.height = height;
			}
			_bitmap = new BitmapData(image.width, image.height, false);
			bitmap.draw(image.parent);
			
			image.smoothing = false;
		}
		
		public function crop(x:int, y:int, width:uint, height:uint):void 
		{
			var rect:Rectangle = new Rectangle(x, y, width, height);
			var bmp:BitmapData = new BitmapData(width, height, false, 0xffffff);
			bmp.copyPixels(bitmap, new Rectangle(x, y, width, height), new Point(0, 0));
			_bitmap = bmp;
		}
		
		override public function upload(url:String):void
		{
			var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
			var request:URLRequest = new URLRequest(url);
			request.requestHeaders.push(header);
			
			request.method = "post";
			
			var encoder:JPGEncoder = new JPGEncoder(quality);
			request.data = encoder.encode(bitmap);
			
			var loader:URLLoader = new URLLoader();
			loader.addEventListener("complete", onUpload);
			loader.load(request);
		}
		
		private function onUpload(e:Event):void 
		{
			e.target.removeEventListener("complete", onUpload);
			dispatchEvent(new FileEvent(FileEvent.UPLOADED, fileName, 1, propagateEvents, propagateEvents));
		}
		
	}
	
}